package rabitmqtest.subscribe;

import java.util.Random;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class EmitLogTopic {

  private static final String EXCHANGE_NAME = "topic_logs";
  
  private static final String[] LEVEL_ARR = {"INFO","WARN","ERROR"};
  private static final String[] USERTYPE_ARR = {"ANONYMOUS","ADMIN","USER"};
  private static final String[] APP_ARR = {"REP","WEIXIN","SALE"};
  
  public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");

      connection = factory.newConnection();
      channel = connection.createChannel();

      channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
      
      while(true) {
    	  String routingKey = getRouting();
          String message = routingKey;

          channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
          System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
          Thread.sleep(2000);
      }
    }
    catch  (Exception e) {
      e.printStackTrace();
    }
    finally {
      if (connection != null) {
        try {
          connection.close();
        }
        catch (Exception ignore) {}
      }
    }
  }

  private static String getRouting(){
	Random r = new Random();
    return LEVEL_ARR[r.nextInt(3)] + "." + USERTYPE_ARR[r.nextInt(3)] + "." + APP_ARR[r.nextInt(3)];
  } 
}